//
//  SISSRecommendedHouseDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/5.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRecommendedHouseDetailViewController.h"
#import "SISSHousingCell.h"
#import "SISSHouseResourceDetailVC.h"
#import "SISSNetworkingManager+PassengerProgress.h"
#import "SISSNetworkingManager+Home.h"

static NSString *houseCellReuseID = @"houseCellReuseID";
@interface SISSRecommendedHouseDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *neighbourhoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndDetailLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelsViewHeightConstraint;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *listTagLabels;
@property (weak, nonatomic) IBOutlet UILabel *propertyStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *averagePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *directionLabel;
@property (weak, nonatomic) IBOutlet UILabel *yearLabel;
@property (weak, nonatomic) IBOutlet UILabel *decorationLabel;
@property (weak, nonatomic) IBOutlet UILabel *floorLabel;
@property (weak, nonatomic) IBOutlet UILabel *housecodingLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherDescriptionLabel;

@property (weak, nonatomic) IBOutlet UIView *propertyCoverView;
@property (weak, nonatomic) IBOutlet UIImageView *jumpImageView;



@property (weak, nonatomic) IBOutlet UITableView *tableView;

/**
 房源模型
 */
@property (strong, nonatomic) SISSHouseResourceModel *houseResourceModel;
/** 房源数组 */
@property (strong, nonatomic) NSArray<SISSHouseResourceModel *> *houseResourceArr;

@end

@implementation SISSRecommendedHouseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 66;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSHousingCell" bundle:nil] forCellReuseIdentifier:houseCellReuseID];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.otherDescriptionLabel.text = @" ";
    // 加载数据
    [self requestHouseResourceData];
    
    for (UILabel *label in self.listTagLabels) {
        label.hidden = YES;
    }
}


- (void)setHouseResourceModel:(SISSHouseResourceModel *)houseResourceModel {
    
    [self.view layoutIfNeeded];
    _houseResourceModel = houseResourceModel;
    
//    if (houseResourceModel.listTag.count > 0) {
//        self.labelsViewHeightConstraint.constant = 20;
//    } else {
//        self.labelsViewHeightConstraint.constant = 1;
//    }
    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    
    for (int i = 0; i < houseResourceModel.listTag.count; i++) {
        UILabel *label = self.listTagLabels[i];
        label.hidden = NO;
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@" %@ ",houseResourceModel.listTag[i].tag];
        NSInteger colorIndex = houseResourceModel.listTag[i].color - 1;
        label.layer.borderColor = labelCololors[colorIndex].CGColor;
        label.textColor = labelCololors[colorIndex];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    self.neighbourhoodLabel.text = houseResourceModel.neighbourhood;
    self.regionAndDetailLabel.text = [NSString stringWithFormat:@"%@-%@",houseResourceModel.region,houseResourceModel.regionDetail];
    
    self.priceLabel.text = [NSString stringWithFormat:@"%0.f万",[houseResourceModel.price floatValue]];
    self.houseTypeLabel.text = houseResourceModel.houseType;
    
    self.areaLabel.text = [NSString stringWithFormat:@"%@㎡",houseResourceModel.area];
    
//    if ([houseResourceModel.propertyState isEqualToString:@"共有产权"]) {
//        self.jumpImageView.hidden = NO;
//        self.propertyCoverView.hidden = NO;
//    } else {
//        self.jumpImageView.hidden = YES;
//        self.propertyCoverView.hidden = YES;
//    }
    
    self.propertyStateLabel.text = houseResourceModel.propertyState;
    self.averagePriceLabel.text = [NSString stringWithFormat:@"%.0f元/㎡",[houseResourceModel.averagePrice floatValue]* 10000];
    self.directionLabel.text = houseResourceModel.direction ? houseResourceModel.direction : @"暂无数据";
    self.yearLabel.text = houseResourceModel.year ? [NSString stringWithFormat:@"%@年",houseResourceModel.year] : @"暂无数据";
    self.decorationLabel.text = houseResourceModel.decoration.length ? houseResourceModel.decoration : @"暂无数据";
    
    NSArray *floorsArr = @[@"1-10层", @"11-20层", @"21-30层", @"30层以上"];
    self.floorLabel.text = houseResourceModel.floor ? floorsArr[houseResourceModel.floor/11] : @"暂无数据";
    self.housecodingLabel.text = houseResourceModel.houseInfoNo;
    self.otherDescriptionLabel.text = houseResourceModel.otherdesc.length ? houseResourceModel.otherdesc : @"暂无数据";
    
}

#pragma mark -- UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.houseResourceArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSHousingCell *cell = [tableView dequeueReusableCellWithIdentifier:houseCellReuseID];
    cell.isHideButton = YES;
    
    cell.houseResourceModel = self.houseResourceArr[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SISSHouseResourceDetailVC *vc = [[SISSHouseResourceDetailVC alloc] init];
    vc.title = @"房源详情";
    vc.isHideBottomButton = YES;
    SISSHouseResourceModel *houseModel = self.houseResourceArr[indexPath.row];
    vc.houseResourceModel = houseModel;
    if (houseModel.read != 1) {
        
        [kNetworkManager setHouseResourceReadWithHouseId:houseModel.ID success:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                NSLog(@"插入成功！");
            }
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    }
    self.houseResourceArr[indexPath.row].read = 1;
    [self.tableView reloadData];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 加载数据
- (void)requestHouseResourceData {
    
    WeakSelf
    [kNetworkManager requestPassengerRecommendHouseWithAdbpId:self.adbpId success:^(NSArray<SISSHouseResourceModel *> *houseResourceArr) {
        
        weakSelf.noDataShowView.hidden = houseResourceArr.count > 0;
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:houseResourceArr.count];
        [houseResourceArr enumerateObjectsUsingBlock:^(SISSHouseResourceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (obj.isMain == 1) {
                weakSelf.houseResourceModel = obj;
            } else {
                [arrM addObject:obj];
            }
        }];
        weakSelf.houseResourceArr = arrM.copy;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


@end
